from django.contrib import admin
from financial import models
from django import forms
# Register your models here.
class InvoicePackagingForm(forms.ModelForm):
    class Meta:
        model = models.InvoicePackagingModel

        fields=['id',  'invoice_num', 'amount','remark' ,'store_orders']

    def clean(self):
        cleaned_data = super().clean()
        store_orders = cleaned_data.get('store_orders')
        print(store_orders)
        if not store_orders:
            raise forms.ValidationError({"store_orders": "至少需要关联一个包材入库单！"})
        else:
            suppliers=set()
            for store_order in store_orders:
                supplier=store_order.supplier
                suppliers.add(supplier)
            if len(suppliers)>1:
                raise forms.ValidationError({"store_orders": "包材入库单要保证同一供应商！"})
        return cleaned_data
@admin.register(models.InvoicePackagingModel)
class InvoivePackagingModelAdmin(admin.ModelAdmin):

    form = InvoicePackagingForm


    list_display =['id', 'invoice_num', 'amount','remark' , 'update_at']


    list_display_links = ('id',)

    '''    
    定义在列表页右侧可用于过滤数据的字段，默认无过滤条件。
    例如设置['state', 'create_at']，则会显示按状态和创建时间过滤的选项。
'''
    list_filter = ['create_at']
    '''    
    控制是否使用select_related优化关联查询（用于外键关联），默认关闭。
    设为True可减少数据库查询次数，提高列表页加载速度（适用于有外键关联的模型）。
    '''
    list_select_related = False

    '''定义列表页每页显示的记录数量，默认 100 条。  '''
    list_per_page = 10
    '''定义 “显示全部” 功能最多可显示的记录数，默认 200 条（超过此数则不显示 “显示全部” 选项）。 simpleui 不支持   '''
    list_max_show_all = 200
    ''' 
    定义列表页中可直接编辑的字段（无需进入详情页），默认无。
    例如设置['state']，则列表中的state字段可直接修改并保存。
   
    
    '''
    search_fields = ('store_orders',)






    readonly_fields = [ 'update_at']  # 关键：将时间字段设为“只读显示”

    # 关键1：使用分栏选择器（左右分栏，更适合多对多选择）filter_horizontal
    filter_horizontal = ['store_orders',]  # 水平分栏，或用 filter_vertical 垂直分栏


class InvoiceRawForm(forms.ModelForm):
    class Meta:
        model = models.InvoiceRawModel

        fields = ['id', 'invoice_num', 'amount', 'remark', 'store_orders']

    def clean(self):
        cleaned_data = super().clean()
        store_orders = cleaned_data.get('store_orders')

        if not store_orders:
            raise forms.ValidationError({"store_orders": "至少需要关联一个包材入库单！"})
        else:
            suppliers = set()
            for store_order in store_orders:
                supplier = store_order.supplier
                suppliers.add(supplier)
            if len(suppliers) > 1:
                raise forms.ValidationError({"store_orders": "包材入库单要保证同一供应商！"})
        return cleaned_data


@admin.register(models.InvoiceRawModel)
class InvoiveRawModelAdmin(admin.ModelAdmin):
    form = InvoiceRawForm
    list_display = ['id', 'invoice_num', 'amount', 'remark', 'update_at']
    list_display_links = ('id',)
    list_filter = ['create_at']
    list_select_related = False
    list_per_page = 10
    list_max_show_all = 200
    search_fields = ('store_orders',)
    readonly_fields = ['update_at']  # 关键：将时间字段设为“只读显示”
    # 关键1：使用分栏选择器（左右分栏，更适合多对多选择）filter_horizontal
    filter_horizontal = ['store_orders', ]  # 水平分栏，或用 filter_vertical 垂直分栏


class InvoiceAdditiveForm(forms.ModelForm):
    class Meta:
        model = models.InvoiceAdditiveModel

        fields = ['id', 'invoice_num', 'amount', 'remark', 'store_orders']

    def clean(self):
        cleaned_data = super().clean()
        store_orders = cleaned_data.get('store_orders')

        if not store_orders:
            raise forms.ValidationError({"store_orders": "至少需要关联一个包材入库单！"})
        else:
            suppliers = set()
            for store_order in store_orders:
                supplier = store_order.supplier
                suppliers.add(supplier)
            if len(suppliers) > 1:
                raise forms.ValidationError({"store_orders": "包材入库单要保证同一供应商！"})
        return cleaned_data


@admin.register(models.InvoiceAdditiveModel)
class InvoiveAdditiveModelAdmin(admin.ModelAdmin):
    form = InvoiceAdditiveForm
    list_display = ['id', 'invoice_num', 'amount', 'remark', 'update_at']
    list_display_links = ('id',)
    list_filter = ['create_at']
    list_select_related = False
    list_per_page = 10
    list_max_show_all = 200
    search_fields = ('store_orders',)
    readonly_fields = ['update_at']  # 关键：将时间字段设为“只读显示”
    # 关键1：使用分栏选择器（左右分栏，更适合多对多选择）filter_horizontal
    filter_horizontal = ['store_orders', ]  # 水平分栏，或用 filter_vertical 垂直分栏
class PaymentRawForm(forms.ModelForm):
    class Meta:
        model = models.PaymentRawModel

        fields = ['id', 'invoice_num', 'amount', 'remark', 'supplier']



@admin.register(models.PaymentRawModel)
class PaymentRawModelAdmin(admin.ModelAdmin):
    form = PaymentRawForm
    list_display = ['id', 'invoice_num', 'amount','supplier', 'remark', 'update_at']
    list_display_links = ('id',)
    list_filter = ['create_at']
    list_select_related = False
    list_per_page = 10
    list_max_show_all = 200
    search_fields = ('supplier',)
    # 关键：启用supplier字段的Admin自动完成搜索
    autocomplete_fields = ['supplier']
    readonly_fields = ['update_at']  # 关键：将时间字段设为“只读显示”

class PaymentAdditiveForm(forms.ModelForm):
    class Meta:
        model = models.PaymentRawModel

        fields = ['id', 'invoice_num', 'amount', 'remark', 'supplier']



@admin.register(models.PaymentAdditiveModel)
class PaymentAdditiveModelAdmin(admin.ModelAdmin):
    form = PaymentAdditiveForm
    list_display = ['id', 'invoice_num', 'amount','supplier', 'remark', 'update_at']
    list_display_links = ('id',)
    list_filter = ['create_at']
    list_select_related = False
    list_per_page = 10
    list_max_show_all = 200
    search_fields = ('supplier',)
    # 关键：启用supplier字段的Admin自动完成搜索
    autocomplete_fields = ['supplier']
    readonly_fields = ['update_at']  # 关键：将时间字段设为“只读显示”

class PaymentPackagingForm(forms.ModelForm):
    class Meta:
        model = models.PaymentPackagingModel

        fields = ['id', 'invoice_num', 'amount', 'remark', 'supplier']



@admin.register(models.PaymentPackagingModel)
class PaymentPackagingModelAdmin(admin.ModelAdmin):
    form = PaymentPackagingForm
    list_display = ['id', 'invoice_num', 'amount','supplier', 'remark', 'update_at']
    list_display_links = ('id',)
    list_filter = ['create_at']
    list_select_related = False
    list_per_page = 10
    list_max_show_all = 200
    search_fields = ('supplier',)
    # 关键：启用supplier字段的Admin自动完成搜索
    autocomplete_fields = ['supplier']
    readonly_fields = ['update_at']  # 关键：将时间字段设为“只读显示”